﻿Imports System.Data.OleDb
Partial Public Class AddNewPet
    Inherits System.Web.UI.Page
    Dim FolderName As String = "Pet_Images/"
    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        Dim qry As String = "INSERT INTO PetDetails (PetName,Type,Breed,Weight,Colour,Age,Advantage,photo,Description,OwnerName) values (@PetName,Type,Breed,Weight,Colour,Age,Advantage,photo,Description,OwnerName)"

        Dim cmd As New OleDbCommand(qry, con)

        Dim Temp As String = Request.MapPath(FolderName & Me.tb_PetPhotoUpld.FileName)
        Me.tb_PetPhotoUpld.SaveAs(Temp)
        Dim S As String = FolderName & Me.tb_PetPhotoUpld.FileName


        cmd.Parameters.AddWithValue("PetName", Me.tb_petName.Text)
        cmd.Parameters.AddWithValue("Type", Me.tb_pettype.Text)
        cmd.Parameters.AddWithValue("Breed", Me.tb_petBreed.Text)
        cmd.Parameters.AddWithValue("Weight", Me.tb_weight.Text)
        cmd.Parameters.AddWithValue("Colour", Me.tb_colour.Text)
        cmd.Parameters.AddWithValue("Age", Me.tb_age.Text)
        cmd.Parameters.AddWithValue("Advantage", Me.tb_advt.Text)

        cmd.Parameters.AddWithValue("photo", S)

        cmd.Parameters.AddWithValue("Description", Me.tb_Description.Text)
        cmd.Parameters.AddWithValue("OwnersName", findUSerID())

        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()

        clear_fields()



    End Sub

    Protected Sub btn_clear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_clear.Click
        clear_fields()
    End Sub
    Function findUSerID()
        Try
            Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
            con.Open()

            Dim oledbcmd As New OleDbCommand("SELECT * From aspnet_Users where UserName = @Username", con)
            oledbcmd.Parameters.AddWithValue("@Username", User.Identity.Name)
            Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
            dr.Read()
            Return dr("UserId")
        Catch ex As Exception
            Return 0
            MsgBox("You must log in to insert data in that", MsgBoxStyle.Critical, "Error!!")
        End Try

    End Function

    Sub clear_fields()
        Me.tb_petName.Text = ""
        Me.tb_weight.Text = ""
        Me.tb_colour.Text = ""
        Me.tb_age.Text = ""
        Me.tb_advt.Text = ""
        Me.tb_dAdvt.Text = ""

        Me.tb_Description.Text = ""
        Me.tb_ownName.Text = ""
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Response.Redirect("List.aspx")
    End Sub
End Class